import numpy as np
import matplotlib.pyplot as plt

def demonstrate_differential_definition():
    """演示微分定义：Δy = AΔx + o(Δx)"""
    
    # 定义函数 f(x) = x²
    f = lambda x: x**2
    x0 = 2
    A = 2*x0  # 对于f(x)=x²，f'(x)=2x，所以A=2x0=4
    
    print("微分定义验证: f(x) = x², x₀ = 2")
    print("=" * 60)
    print("Δx\t\tΔy\t\tAΔx\t\to(Δx)\t\to(Δx)/Δx")
    
    # 测试不同大小的Δx
    delta_x_values = [0.5, 0.1, 0.01, 0.001, 0.0001]
    
    for dx in delta_x_values:
        delta_y = f(x0 + dx) - f(x0)  # 实际增量
        A_dx = A * dx                 # 线性主部（微分）
        o_dx = delta_y - A_dx         # 高阶无穷小
        ratio = o_dx / dx if dx != 0 else 0
        
        print(f"{dx:.4f}\t{delta_y:.6f}\t{A_dx:.6f}\t{o_dx:.8f}\t{ratio:.8f}")

demonstrate_differential_definition()